﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Icms.Core.Model;
using Icms.Core.Repository;
using Omu.Awesome.Mvc;

namespace Icms.WebUI.Controllers
{
    public class ParametersLookupController : LookupController
    {
        private readonly IRepo<Parameter> r;

        public ParametersLookupController(IRepo<Parameter> r)
        {
            this.r = r;
        }

        public override ActionResult SearchForm()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Search(string search, IEnumerable<Guid> selected, Guid id, Guid? sClassifier)
        {
            selected = selected ?? Enumerable.Empty<Guid>();
            var result = r.Where(o => (sClassifier == null || o.Classifier.Id == sClassifier) 
                && o.Title.StartsWith(search) && o.Id != id && !selected.Contains(o.Id));

            return View(@"Awesome\LookupList", result);
        }

        public ActionResult Selected(IEnumerable<Guid> selected)
        {
            var result = selected == null ? Enumerable.Empty<Parameter>() : r.Where(o => selected.Contains(o.Id));
            return View(@"Awesome\LookupList", result);
        }

        public ActionResult GetMultiple(IEnumerable<Guid> selected)
        {
            return Json(r.GetAll().Where(o => selected.Contains(o.Id)).Select(v => new { Text = v.Title }));
        }
    }
}